package com.nl.controller.web.controller;


import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.nl.controller.common.model.Response;
import com.nl.controller.common.service.CommonService;
import com.nl.controller.common.util.ResponseUtil;
import com.nl.controller.web.bean.SmsUsers;
import com.nl.controller.web.service.LoginService;
import com.nl.controller.web.service.SmsMgrTemplateInfoService;
import com.nl.controller.web.service.SmsTemplateCheckService;
import com.nl.controller.web.service.SmsUserService;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
@RequestMapping("templateCheck")
public class SmsTemplateCheckController {
    @Autowired
    private SmsTemplateCheckService smsTemplateCheckService;
    @Autowired
    private SmsMgrTemplateInfoService smsMgrTemplateInfoService;
    @Autowired
    private CommonService commonService;
    @Autowired
    private LoginService loginService;
    @Autowired
    private SmsUserService smsUserService;
    
    @ApiOperation(value = "模板审核", notes = "模板审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mgrId", value = "项目编号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "state", value = "审核结果", required = true, dataType = "String"),
            @ApiImplicitParam(name = "remark", value = "审核描述", required = false, dataType = "String"),
    })
    @PostMapping("check")
    public Response checkTemlplate(
            @RequestParam(value = "mgrId", required = true) String mgrId,
            @RequestParam(value = "state", required = true) String state,
            @RequestParam(value = "remark", required = true) String remark,
            HttpSession session,
            @RequestParam Map<String, Object> paramMap
    ) {
        int result;
        log.info("模板审核参数:{}" + paramMap.toString());
        SmsUsers users = (SmsUsers) session.getAttribute("user");
        result = smsTemplateCheckService.templateCheck(mgrId, state, remark, users.getMsisdn());
        long mgrState=Long.parseLong(smsMgrTemplateInfoService.findMgrTemplateByMgrId(mgrId).getMgrState());
        if (1 == result) {
            StringBuffer sbf = new StringBuffer();
            sbf.append("项目:");
            sbf.append(smsMgrTemplateInfoService.findMgrTemplateByMgrId(mgrId).getName());
            sbf.append("已完成审核，审核结果：");
            //发送给省公司管理员
            if (("2").equals(state)) {
                sbf.append("成功，审核理由:");
            } else {
                sbf.append("失败，审核理由:");
            }
            sbf.append(remark);
            System.out.println("审核成功==================="+sbf.toString());
            String[] msisdn = commonService.templateNotice("1000000001");
            int result1 = 0;
            if (msisdn != null && msisdn.length > 0) {
                for (String number : msisdn) {
                	result1 = loginService.sendCode(number,sbf.toString());
                    log.info("发送的手机号码" + number);
                    log.info("内容" + sbf.toString());
                    if (result1 !=1) {
                    	result1 = loginService.sendCode(number,sbf.toString());
					}
                }
            }
            //发送给项目管理员
            int result2 = 0;
            for (SmsUsers user : smsUserService.getMsisdn(mgrId)) {
                loginService.sendCode(user.getMsisdn(), sbf.toString());
                if (result2 !=1) {
                	result1 = loginService.sendCode(user.getMsisdn(), sbf.toString());
				}
                log.info("发送的手机号码" + user.getMsisdn());
                log.info("内容" + sbf.toString());
            }
                
            return ResponseUtil.sucess("审核成功",mgrState);
        } else {
            return ResponseUtil.sucess("审核失败请重试");
        }
    }
}
